{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 矩阵标量乘法\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c77654c5-82c0-49e9-ae19-701d7f63e454",
   "metadata": {},
   "source": [
    "该代码使用`numpy`库定义了一个列向量、一个行向量和一个矩阵，并分别对它们执行标量乘法操作。标量乘法是一种基本的线性代数运算，即将向量或矩阵的每个元素与同一数相乘。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **列向量标量乘法**：定义列向量\n",
    "\n",
    "   $$\n",
    "   a_\\text{col} = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   将该向量与标量$2$相乘，得到新的列向量\n",
    "\n",
    "   $$\n",
    "   b_\\text{col} = 2 \\times a_\\text{col} = \\begin{bmatrix} 2 \\\\ 4 \\\\ 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. **行向量标量乘法**：定义行向量\n",
    "\n",
    "   $$\n",
    "   a_\\text{row} = \\begin{bmatrix} 1 & 2 & 3 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   将该向量与标量$2$相乘，结果为\n",
    "\n",
    "   $$\n",
    "   b_\\text{row} = 2 \\times a_\\text{row} = \\begin{bmatrix} 2 & 4 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "3. **矩阵标量乘法**：定义矩阵\n",
    "\n",
    "   $$\n",
    "   A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   通过将每个元素与$2$相乘，得到新矩阵\n",
    "\n",
    "   $$\n",
    "   B = 2 \\times A = \\begin{bmatrix} 2 & 4 & 6 \\\\ 8 & 10 & 12 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "### 总结\n",
    "\n",
    "这段代码展示了列向量、行向量和矩阵的标量乘法。通过这种操作，可以将向量和矩阵中的每个元素同时放大或缩小，这在数据缩放、线性变换和各种线性代数运算中具有广泛应用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "266b0921-1792-4003-ac95-409826a1dcce",
   "metadata": {},
   "source": [
    "## 定义并操作列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "754d4f7e-c422-405c-96a4-27f492433f26",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "197ad134-7e1a-453e-b30b-8138f1d63e19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_col = np.array([[1], [2], [3]])  # 定义列向量a_col\n",
    "a_col  # 输出列向量a_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9c631466-e075-47a5-8e96-747fa1516cc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2],\n",
       "       [4],\n",
       "       [6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_col = 2 * a_col  # 将列向量a_col乘以2得到新列向量b_col\n",
    "b_col  # 输出新列向量b_col"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8022796-f6c6-45f9-a27f-2d77eeca5e39",
   "metadata": {},
   "source": [
    "## 定义并操作行向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab437155-59c7-4253-a3a3-ecccbf9e1dad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_row = np.array([[1, 2, 3]])  # 定义行向量a_row\n",
    "a_row  # 输出行向量a_row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d3519acb-6378-4531-8a62-06fb91819a28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4, 6]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_row = 2 * a_row  # 将行向量a_row乘以2得到新行向量b_row\n",
    "b_row  # 输出新行向量b_row"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77599f82-bb49-4f14-af94-bdcfc17037f8",
   "metadata": {},
   "source": [
    "## 定义并操作矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "71b91160-25fc-4c3c-980d-a4d454f74928",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3],  # 定义矩阵A\n",
    "              [4, 5, 6]])\n",
    "A  # 输出矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e3416055-f508-4037-8202-921d2a5772e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [ 8, 10, 12]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = 2 * A  # 将矩阵A乘以2得到新矩阵B\n",
    "B  # 输出新矩阵B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
